<?php

//tailvh@gmail.com

//Require model files
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Importbill.php';
//Require objects files
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Importbill.php';
//Require forms files
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Importbill.php';

class ImportbillController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private  $modelImportbill; 
    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////

    public function preDispatch() {

    }

    public function init() {
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        
        Zend_Layout::startMvc($option);
        $this->modelImportbill = new End_Models_Importbill(); 
        $this->_redirector = $this->_helper->getHelper('Redirector');
   }

    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    //INDEX ACTION
    public function indexAction() { 
        $tmpArrImportbill = $this->modelImportbill->fetchAll();
        $this->view->arrImportbill = $tmpArrImportbill; 
    }

    public function addAction(){ 
        $formImportbill = new Application_Form_Importbill();
        if ($this->getRequest()->isGet()) { 
            $formImportbill->importbill_id->setValue($Importbill_id);
        } else {
            $Cancel = $this->getRequest()->getPost('cancel', '0');
            if ($Cancel) {
                $this->_redirector->gotoUrl('importbill/index');
            }
            $formData = $this->getRequest()->getPost();
            if ($formImportbill->isValid($formData)) { 
                $toDate = date('Y-m-d');               
                $importbill = new Importbill($formData['importbill_id'], $formData['user_id_employee'], $toDate);
               
                $this->modelImportbill->_insert($importbill);
                $this->_redirector->gotoUrl('importbill/index');
            } else { 
                $formImportbill->populate($formData);
            }
        }
        $this->view->form = $formImportbill;
    }
    
    public function deleteAction(){
        $id = $this->_getParam('id','0');
        $Importbill = $this->modelImportbill->_find($id);
        if(isset($Importbill))
        {
            $this->modelImportbill->_delete($id);
            $this->_redirector->gotoUrl('importbill/index');
        }
        
        $this->_redirector->gotoUrl('importbill/index'); 
    }
    
    public function editAction(){
        $id = $this->_getParam('id','0');
        $Importbill = $this->modelImportbill->_find($id);
        $formImportbill = new Application_Form_Importbill();
        
        if (!$this->getRequest()->isGet())  
        {
            if(isset($Importbill))
            {               
                $Cancel = $this->getRequest()->getPost('cancel', '0');
                if ($Cancel) {
                    $this->_redirector->gotoUrl('importbill/index');
                }

                $formData = $this->getRequest()->getPost();
                if ($formImportbill->isValid($formData)) { 
                    $toDate = date('Y-m-d');                   
                    $Importbill->importbill_id         = $formData['importbill_id'];
                    $Importbill->user_id_employee      = $formData['user_id_employee'];
                    $Importbill->importbill_date_created = $toDate;
                            
                    $id = $this->modelImportbill->_update($Importbill);
                            
                    $this->_redirector->gotoUrl('importbill/index');
                } else { 
                    $formImportbill->populate($formData);
                }
            }else
            {
                $this->_redirector->gotoUrl('importbill/index');
            }    
            $this->view->form = $formImportbill;
        }    
        else                                                                    //lần đầu gọi tới ta sẽ gán giá trị của đối tượng lên form
        {       
            if(isset($Importbill))                                                 //nếu đối tượng tồn tại
            {
                $UC = new Utility_Converter();                                  //tạo công cụ giúp đổi đối tượng thành mảng
                $formImportbill->populate($UC->objectToArray($Importbill));           //đổi đối tượng thành mảng rồi gán cho form vừa tạo
                $this->view->form = $formImportbill;
            }
            else
            {
                $this->_redirector->gotoUrl('importbill/index');   
            }
        }
    }
    
}